﻿/*
 ******************************************************************
 *      
 * 域名空间　	: GreatB2C.DALSQLServer
 * 类　名　称	: ComplainItem.cs
 * 类　功　能	: 数据操作实现层
 * 类　流　程    :                             
 * 运用算法　	:                             
 * 编　写　人	: WHL             
 * 编写日期　	: 2011-3-28
 * 最近更新日期　:
 * 版　　本　	: V1.0                           
 * 描　　述　	: Descript of Class                                                 
 *                                        
 ******************************************************************
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using GreatB2C.Model;
using GreatB2C.DALFactory;
using GreatB2C.DALProfile;
using GreatB2C.Config;
using GreatB2C.Utility;

namespace GreatB2C.DALSQLServer
{
    /// <summary>
    /// 类描述：
    /// </summary>
    [Serializable]
    public class ComplainItem : DbBase, IComplainItem
    {
        #region implement methods

        /// <summary>
        /// 获取参数参数列表
        /// </summary>
        /// <param name="ac"></param>
        /// <returns></returns>
        private SqlParameter[] GetParameters(GreatB2C.Model.ComplainItem ac)
        {
            int i = 0;
            SqlParameter[] param = new SqlParameter[0];

            param[i] = new SqlParameter("@ItemID", SqlDbType.Int, 4);
            param[i++].Value = ac.ItemID;

            param[i] = new SqlParameter("@ClientID", SqlDbType.Int, 4);
            param[i++].Value = ac.ClientID;

            param[i] = new SqlParameter("@ContacterID", SqlDbType.Int, 4);
            param[i++].Value = ac.ContacterID;

            param[i] = new SqlParameter("@ComplainType", SqlDbType.Int, 4);
            param[i++].Value = ac.ComplainType;

            param[i] = new SqlParameter("@ComplainMode", SqlDbType.Int, 4);
            param[i++].Value = ac.ComplainMode;

            param[i] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.Title;

            param[i] = new SqlParameter("@Content", SqlDbType.NVarChar, 16);
            param[i++].Value = ac.Content;

            param[i] = new SqlParameter("@FirstReceiver", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.FirstReceiver;

            param[i] = new SqlParameter("@DateAndTime", SqlDbType.DateTime, 8);
            param[i++].Value = ac.DateAndTime;

            param[i] = new SqlParameter("@MagnitudeOfExigence", SqlDbType.Int, 4);
            param[i++].Value = ac.MagnitudeOfExigence;

            param[i] = new SqlParameter("@Process", SqlDbType.NVarChar, 16);
            param[i++].Value = ac.Process;

            param[i] = new SqlParameter("@Processor", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.Processor;

            param[i] = new SqlParameter("@Result", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.Result;

            param[i] = new SqlParameter("@EndTime", SqlDbType.DateTime, 8);
            param[i++].Value = ac.EndTime;

            param[i] = new SqlParameter("@Feedback", SqlDbType.NVarChar, 255);
            param[i++].Value = ac.Feedback;

            param[i] = new SqlParameter("@ConfirmTime", SqlDbType.DateTime, 8);
            param[i++].Value = ac.ConfirmTime;

            param[i] = new SqlParameter("@ConfirmCaller", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.ConfirmCaller;

            param[i] = new SqlParameter("@ConfirmScore", SqlDbType.Int, 4);
            param[i++].Value = ac.ConfirmScore;

            param[i] = new SqlParameter("@ConfirmFeedback", SqlDbType.NVarChar, 255);
            param[i++].Value = ac.ConfirmFeedback;

            param[i] = new SqlParameter("@Status", SqlDbType.Int, 4);
            param[i++].Value = ac.Status;

            param[i] = new SqlParameter("@CurrentOwner", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.CurrentOwner;

            param[i] = new SqlParameter("@Remark", SqlDbType.NVarChar, 255);
            param[i++].Value = ac.Remark;

            param[i] = new SqlParameter("@Defendant", SqlDbType.NVarChar, 50);
            param[i++].Value = ac.Defendant;


            return param;
        }

        /// <summary>
        /// 检查是否有重复记录。
        /// 注意：这里要手动修改一下，需要加入次主键做为第二条件（主要是为了修改操作）
        /// </summary>
        /// <param name="objModel">对应的数据映射类</param>
        /// <returns>操作结果：true-成功 false-失败</returns>
        public bool IsExists(GreatB2C.Model.ComplainItem objModel)
        {
            string Sql = "SELECT Count(*) "
                            + " FROM "
                            + "[dbo].[" + Prefix + "ComplainItem]"
                            + " WHERE "
                            + "	[ItemID] = @ItemID"
                //+" and 次主键=@次主键"
                            ;

            try
            {
                SqlParameter[] param = GetParameters(objModel);

                int count = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, param).ToString());
                if (count > 0)
                {
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 检索出表中所有记录	
        /// </summary>
        /// <param name="aryResult">返回对象数据集合</param>
        /// <returns>0-操作成功 , errnum-操作失败，返回错误数</returns>
        public DataTable ListComplainItem()
        {
            DataTable dtData = new DataTable();
            string Sql = "SELECT "
                            + "[ItemID],"
                            + "[ClientID],"
                            + "[ContacterID],"
                            + "[ComplainType],"
                            + "[ComplainMode],"
                            + "[Title],"
                            + "[Content],"
                            + "[FirstReceiver],"
                            + "[DateAndTime],"
                            + "[MagnitudeOfExigence],"
                            + "[Process],"
                            + "[Processor],"
                            + "[Result],"
                            + "[EndTime],"
                            + "[Feedback],"
                            + "[ConfirmTime],"
                            + "[ConfirmCaller],"
                            + "[ConfirmScore],"
                            + "[ConfirmFeedback],"
                            + "[Status],"
                            + "[CurrentOwner],"
                            + "[Remark],"
                            + "[Defendant]"
                            + " FROM "
                            + "[dbo].[" + Prefix + "ComplainItem]";

            try
            {
                return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 根据表中主键ID查询记录信息
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <param name="DesModel">输出的对象映射模型</param>
        /// <returns>0-操作成功 , errnum-操作失败，返回错误数</returns>
        public GreatB2C.Model.ComplainItem ListByID(GreatB2C.Model.ComplainItem objModel)
        {
            string Sql = "SELECT * "
                            + " FROM "
                            + "[dbo].[" + Prefix + "ComplainItem]"
                            + " WHERE "
                            + "	[ItemID] = @ItemID";

            try
            {
                SqlParameter[] param = GetParameters(objModel);

                using (DataTable dtData = DbHelper.ExecuteTable(CommandType.Text, Sql, param))
                {
                    if (dtData.Rows.Count > 0)
                    {
                        GreatB2C.Model.ComplainItem modClass = new GreatB2C.Model.ComplainItem();

                        modClass.ItemID = Convert.ToInt32(dtData.Rows[0]["ItemID"].ToString());
                        modClass.ClientID = Convert.ToInt32(dtData.Rows[0]["ClientID"].ToString());
                        modClass.ContacterID = Convert.ToInt32(dtData.Rows[0]["ContacterID"].ToString());
                        modClass.ComplainType = Convert.ToInt32(dtData.Rows[0]["ComplainType"].ToString());
                        modClass.ComplainMode = Convert.ToInt32(dtData.Rows[0]["ComplainMode"].ToString());
                        modClass.Title = dtData.Rows[0]["Title"].ToString();
                        modClass.Content = dtData.Rows[0]["Content"].ToString();
                        modClass.FirstReceiver = dtData.Rows[0]["FirstReceiver"].ToString();
                        modClass.DateAndTime = Convert.ToDateTime(dtData.Rows[0]["DateAndTime"].ToString());
                        modClass.MagnitudeOfExigence = Convert.ToInt32(dtData.Rows[0]["MagnitudeOfExigence"].ToString());
                        modClass.Process = dtData.Rows[0]["Process"].ToString();
                        modClass.Processor = dtData.Rows[0]["Processor"].ToString();
                        modClass.Result = dtData.Rows[0]["Result"].ToString();
                        modClass.EndTime = Convert.ToDateTime(dtData.Rows[0]["EndTime"].ToString());
                        modClass.Feedback = dtData.Rows[0]["Feedback"].ToString();
                        modClass.ConfirmTime = Convert.ToDateTime(dtData.Rows[0]["ConfirmTime"].ToString());
                        modClass.ConfirmCaller = dtData.Rows[0]["ConfirmCaller"].ToString();
                        modClass.ConfirmScore = Convert.ToInt32(dtData.Rows[0]["ConfirmScore"].ToString());
                        modClass.ConfirmFeedback = dtData.Rows[0]["ConfirmFeedback"].ToString();
                        modClass.Status = Convert.ToInt32(dtData.Rows[0]["Status"].ToString());
                        modClass.CurrentOwner = dtData.Rows[0]["CurrentOwner"].ToString();
                        modClass.Remark = dtData.Rows[0]["Remark"].ToString();
                        modClass.Defendant = dtData.Rows[0]["Defendant"].ToString();

                        return modClass;
                    }
                }

                return null;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 新增一条新记录到表中
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>0-操作成功 , errnum-操作失败，返回错误数</returns>
        public bool Insert(GreatB2C.Model.ComplainItem objModel)
        {
            string Sql = "INSERT INTO "
                        + "[dbo].[" + Prefix + "ComplainItem]"
                        + "("
                        + "[ClientID],"
                        + "[ContacterID],"
                        + "[ComplainType],"
                        + "[ComplainMode],"
                        + "[Title],"
                        + "[Content],"
                        + "[FirstReceiver],"
                        + "[DateAndTime],"
                        + "[MagnitudeOfExigence],"
                        + "[Process],"
                        + "[Processor],"
                        + "[Result],"
                        + "[EndTime],"
                        + "[Feedback],"
                        + "[ConfirmTime],"
                        + "[ConfirmCaller],"
                        + "[ConfirmScore],"
                        + "[ConfirmFeedback],"
                        + "[Status],"
                        + "[CurrentOwner],"
                        + "[Remark],"
                            + "[Defendant]"
                        + ") VALUES ("
                        + "@ClientID,"
                        + "@ContacterID,"
                        + "@ComplainType,"
                        + "@ComplainMode,"
                        + "@Title,"
                        + "@Content,"
                        + "@FirstReceiver,"
                        + "@DateAndTime,"
                        + "@MagnitudeOfExigence,"
                        + "@Process,"
                        + "@Processor,"
                        + "@Result,"
                        + "@EndTime,"
                        + "@Feedback,"
                        + "@ConfirmTime,"
                        + "@ConfirmCaller,"
                        + "@ConfirmScore,"
                        + "@ConfirmFeedback,"
                        + "@Status,"
                        + "@CurrentOwner,"
                        + "@Remark,"
                            + "@Defendant"
                        + ")";

            try
            {
                SqlParameter[] param = GetParameters(objModel);

                int affect = DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);

                if (affect == 1)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 根据主键ID更新指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>0-操作成功 , errnum-操作失败，返回错误数</returns>
        public bool Update(GreatB2C.Model.ComplainItem objModel)
        {
            string Sql = "UPDATE "
                        + "[dbo].[" + Prefix + "ComplainItem]"
                        + " SET "
                        + "[ClientID] = @ClientID,"
                        + "[ContacterID] = @ContacterID,"
                        + "[ComplainType] = @ComplainType,"
                        + "[ComplainMode] = @ComplainMode,"
                        + "[Title] = @Title,"
                        + "[Content] = @Content,"
                        + "[FirstReceiver] = @FirstReceiver,"
                        + "[DateAndTime] = @DateAndTime,"
                        + "[MagnitudeOfExigence] = @MagnitudeOfExigence,"
                        + "[Process] = @Process,"
                        + "[Processor] = @Processor,"
                        + "[Result] = @Result,"
                        + "[EndTime] = @EndTime,"
                        + "[Feedback] = @Feedback,"
                        + "[ConfirmTime] = @ConfirmTime,"
                        + "[ConfirmCaller] = @ConfirmCaller,"
                        + "[ConfirmScore] = @ConfirmScore,"
                        + "[ConfirmFeedback] = @ConfirmFeedback,"
                        + "[Status] = @Status,"
                        + "[CurrentOwner] = @CurrentOwner,"
                        + "[Remark] = @Remark,"
                            + "[Defendant] = @Defendant"
                        + " WHERE "
                        + "[ItemID] = @ItemID"
                        ;

            try
            {
                SqlParameter[] param = GetParameters(objModel);

                int affect = DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);

                if (affect == 1)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 根据主键删除指定记录
        /// </summary>
        /// <param name="objModel">相应的对象映射模型</param>
        /// <returns>0-操作成功 , errnum-操作失败，返回错误数</returns>
        public bool Delete(GreatB2C.Model.ComplainItem objModel)
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "ComplainItem]"
                            + " WHERE "
                            + "[ItemID] = @ItemID"
                            ;

            try
            {
                SqlParameter[] param = GetParameters(objModel);

                int affect = DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);

                if (affect == 1)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 根据ID列表批量删除记录
        /// 数据格式：1,2,3,4,5
        /// </summary>
        /// <param name="strList">ID列表</param>
        /// <returns>0-操作成功 , errnum-操作失败，返回错误数</returns>
        public bool DeleteList(string strList)
        {
            string Sql = "DELETE FROM "
                            + "[dbo].[" + Prefix + "ComplainItem]"
                            + " WHERE "
                            + "[ItemID] in ('" + strList + "')"
                            ;

            try
            {
                int affect = DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);

                if (affect == 1)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                return false;
            }
        }

        #endregion
    }
}
